package com.zhy.baselibrary.api

import android.util.Log
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.MediaType
import okhttp3.RequestBody
import java.util.*

public class DataUtils<T> private constructor() {
    companion object {
        private var dataUtils: DataUtils<*>? = null
        fun init(): DataUtils<*> {
            if (dataUtils == null) dataUtils = DataUtils<Any>()
            return dataUtils!!
        }
    }

    //数据加密
    fun request(map: HashMap<String, Any?>?): RequestBody {
        var mapNew = map ?: HashMap<String, Any?>()
        val gson = GsonBuilder().disableHtmlEscaping().create()
        var json = gson.toJson(mapNew)
        Log.d("请求原数据","$json")
        return RequestBody.create(MediaType.get("application/json; charset=utf-8"), json)
    }


    fun <T> jsonDecoders(json: String, clazz: Class<T>): T {
        return Gson().fromJson<T>(json, clazz)
    }


    fun <T> jsonDecoder(json: String, clazz: Class<T>): T {
        return Gson().fromJson<T>(json, clazz)
    }

}